package com.hxzy.springbootrabbitmqproducer.controller;

import com.alibaba.fastjson.JSONObject;
import com.hxzy.springbootrabbitmqproducer.dto.UserDTO;
import lombok.extern.log4j.Log4j2;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 类说明
 *
 * @author admin
 * @date 2021-07-22
 * finished
 */
@Log4j2
@RestController
public class HelloController {


    /**
     * 消息队列模板 -->springframework.amqp.core
     */
    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 生产者生产消费
     * @param message
     * @return
     */
    @GetMapping(value = "/send")
    public String sendMessage(String message){
       log.debug("----消息队列生产者发送了消息："+message);
        log.debug("检查库存的 service");
        log.debug("下订单的service");

        // 异步 、解耦
       amqpTemplate.convertAndSend("hxzy_hello", message);
       return "生产者发送消息成功了";
    }


    /**
     * 生产者生产消费
     * @param user
     * @return
     */
    @GetMapping(value = "/sendUser")
    public String sendMessage(UserDTO user){
        log.debug("发送对象:"+user.toString());

        String jsonStr= JSONObject.toJSONString(user);

        // 异步 、解耦
        amqpTemplate.convertAndSend("hxzy_hello", jsonStr);
        return "生产者发送消息成功了";
    }

}
